<script setup>
import FirstTitle from "@comp/FirstTitle";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import { onMounted } from "vue";
import { searchDataZymb } from "@/api/search";
import { addOrUpdateZymb } from "@/api/addOrUpdate";

const city = window.globalObj.name;
const editorRef = shallowRef();

const toolbarConfig = {};
const editorConfig = {
  placeholder: "请输入内容...",
};
const mode = ref("default");

// 加载html
const initHtml = () => {
  searchDataZymb({ m: "组织指挥-协同组织" }).then((res) => {
    const { data } = res;
    valueHtml.value = data.find((x) => x.k === "协同组织")?.v || "";
  });
};

const handleCreated = (editor) => {
  editorRef.value = editor; // 记录 editor 实例，重要！
};

const valueHtml = ref(
  "基本原则：①防反结合，以防为主，突出关键部位，加强重点防护。②综合防护，因地制宜，充分运用信息防护、搬迁转移、加固伪装、干扰诱偏等多种防护手段，最大限度地提升重要目标的生存能力。③突出重点，集中力量，快速抢救抢修，力求尽快恢复目标的主要功能。<br/>基本方法：①采取计划协同和随机协同相结合的方式组织重要目标防护的协同组织，目标遭遇空袭前通常按计划协同为主；目标遭遇空袭后，在按计划行动的同时，快速随机协同；②采取多种手段，充分发挥信息系统功能，广泛利用文电传输、信息共享、视频通联等多种方式，开展线上线下协同组织，切实提高协同组织的实效性；③采取各目标单位指挥为主，必要时，属地行政机构派出一线指挥组现地督导组织协同，切实增强协同组织的针对性、实效性和连续性。"
);

const saveProgramme = () => {
  ElMessageBox.confirm(`确定要保存吗?`, "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
    customClass: "message-box",
  }).then(() => {
    addOrUpdateZymb({
      m: "组织指挥-协同组织",
      data: [
        {
          k: "协同组织",
          v: valueHtml.value,
        },
      ],
    })
      .then(() => {
        ElMessage.success("操作成功");
      })
      .catch(() => {});
  });
};

onMounted(() => {
  initHtml();
});
onBeforeUnmount(() => {
  const editor = editorRef.value;
  if (!editor) return;
  editor.destroy();
});
</script>
<template>
  <div class="">
    <FirstTitle name="指挥关系" />
    <div style="border: 1px solid #ccc" class="top-15">
      <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editorRef"
        :defaultConfig="toolbarConfig"
        :mode="mode"
      />
      <Editor
        style="height: 500px; overflow-y: hidden"
        v-model="valueHtml"
        :defaultConfig="editorConfig"
        :mode="mode"
        @onCreated="handleCreated"
      />
    </div>

    <div class="flex-justify-end top-20">
      <el-button type="primary" class="save-btn" @click="saveProgramme"
        >保存</el-button
      >
    </div>
  </div>
</template>
<style scoped lang="scss"></style>